package com.waar.kickball.lineupimage;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import com.waar.kickball.positions.Positions;

/**
 * @author Lance Burk
 *Copyright Lance + Craig 2013
 * 
 */
public class LineUpImageWriter {
	
	private BufferedImage image;

	public LineUpImageWriter(){
		try {
			File file = new File(System.getProperty("user.dir") + "/conf/lineup.png");
            image = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
		
		drawTextOnImage(image);
	}
	
	public void drawTextOnImage(BufferedImage image){
		int w = image.getWidth();
		int h = image.getHeight();
		BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = img.createGraphics();
		g2d.drawImage(image, 0, 0, null);
		g2d.setPaint(Color.red);
		g2d.setFont(new Font("Serif", Font.PLAIN, 20));
		String s = "Fuck you Craig";
		for(Positions position: Positions.values()){
			drawFirstInning(g2d, position.getImageBoxOffsetWidth(), position.getImageBoxOffsetHeight(), s);
			drawSecondInning(g2d, position.getImageBoxOffsetWidth(), position.getImageBoxOffsetHeight(), s);
			drawThirdInning(g2d, position.getImageBoxOffsetWidth(), position.getImageBoxOffsetHeight(), s);
			drawFourthInning(g2d, position.getImageBoxOffsetWidth(), position.getImageBoxOffsetHeight(), s);
			drawFifthInning(g2d, position.getImageBoxOffsetWidth(), position.getImageBoxOffsetHeight(), s);
		}
		
		g2d.dispose();
		File file = new File(System.getProperty("user.dir") + "/conf/outputLineup.png");
		
		try {
			if(file.exists()) {
				file.createNewFile();
			}
			ImageIO.write(img, "PNG", file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void drawFirstInning(Graphics2D g2d, int x, int y, String name){
		g2d.drawString(name, x, y);
	}
	
	public void drawSecondInning(Graphics2D g2d, int x, int y, String name){
		g2d.drawString(name, x, y+28);
	}
	
	public void drawThirdInning(Graphics2D g2d, int x, int y, String name){
		g2d.drawString(name, x+185, y-31);
	}
	
	public void drawFourthInning(Graphics2D g2d, int x, int y, String name){
		g2d.drawString(name, x+185, y);
	}
	
	public void drawFifthInning(Graphics2D g2d, int x, int y, String name){
		g2d.drawString(name, x+185, y+28);
	}
}
